/*
 * This file is part of animus, a GUI for the command line app "GHost++"
 *
 * http://code.google.com/p/animus/
 *
 * Copyright (C) 2008-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace animus
{
    public partial class FormTriggers : Form
    {
        ConsoleTriggerCollection m_Triggers;
        public FormTriggers(ConsoleTriggerCollection triggers)
        {
            m_Triggers = triggers;
            InitializeComponent();
            foreach (ConsoleTrigger t in m_Triggers)
            {
                AddTrigger(t);
            }
        }

        private void AddTrigger(ConsoleTrigger trigger)
        {
            ListViewItem i = new ListViewItem(trigger.Name);
            if (trigger.GetType() == typeof(ConsoleTriggerFontChange))
                i.ImageKey = "font";
            else if (trigger.GetType() == typeof(ConsoleTriggerTrayBalloon))
                i.ImageKey = "popup";
            else if (trigger.GetType() == typeof(ConsoleTriggerPlaySound))
                i.ImageKey = "sound";
            else if (trigger.GetType() == typeof(ConsoleTriggerSendKeys))
                i.ImageKey = "hotkey";
            i.Tag = trigger;
            i.SubItems.Add(trigger.GetType().Name);
            i.SubItems.Add(trigger.TriggerText);
            i.SubItems.Add(trigger.GetParameters());
            lwTriggers.Items.Add(i);
        }

        private void UpdateTrigger(ListViewItem i, ConsoleTrigger t)
        {
            i.Tag = t;
            i.Text = t.Name;
            i.SubItems[1].Text = t.GetType().Name;
            i.SubItems[2].Text = t.TriggerText;
            i.SubItems[3].Text = t.GetParameters();
        }

        private void bAddTrigger_Click(object sender, EventArgs e)
        {
            using (FormEditTrigger trigger = new FormEditTrigger())
            {
                ConsoleTrigger t = trigger.ShowDialog(null);
                if (t != null)
                {
                    AddTrigger(t);
                    m_Triggers.Add(t);
                }
            }
        }

        private void lwTriggers_DoubleClick(object sender, EventArgs e)
        {
            foreach (ListViewItem i in lwTriggers.SelectedItems)
            {
                using (FormEditTrigger t = new FormEditTrigger())
                {
                    ConsoleTrigger trigger = t.ShowDialog((ConsoleTrigger)i.Tag);
                    if (trigger != null)
                    {
                        m_Triggers.Update((ConsoleTrigger)i.Tag, trigger);
                        UpdateTrigger(i, trigger);
                    }
                }
            }
        }

        private void bRemoveTrigger_Click(object sender, EventArgs e)
        {
            while(lwTriggers.SelectedItems.Count > 0)
            {
                m_Triggers.Remove((ConsoleTrigger)lwTriggers.SelectedItems[0].Tag);
                lwTriggers.SelectedItems[0].Remove();
            }
        }

        private void bMoveTriggerUp_Click(object sender, EventArgs e)
        {
            ListViewItem[] items = new ListViewItem[lwTriggers.SelectedItems.Count];
            lwTriggers.SelectedItems.CopyTo(items, 0);
            for (int i=0; i<items.Length; i++)
            {
                if (items[i].Index>0)
                {
                    ListViewItem oldItem = lwTriggers.Items[items[i].Index - 1];
                    lwTriggers.Items.Remove(oldItem);
                    m_Triggers.Remove((ConsoleTrigger)oldItem.Tag);
                    lwTriggers.Items.Insert(items[i].Index + 1, oldItem);
                    m_Triggers.Insert(items[i].Index + 1, (ConsoleTrigger)oldItem.Tag);
                    items[i].Selected = true;
                }
            }
        }

        private void bMoveTriggerDown_Click(object sender, EventArgs e)
        {
            ListViewItem[] items = new ListViewItem[lwTriggers.SelectedItems.Count];
            lwTriggers.SelectedItems.CopyTo(items, 0);
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i].Index < lwTriggers.Items.Count - 1)
                {
                    ListViewItem oldItem = lwTriggers.Items[items[i].Index + 1];
                    lwTriggers.Items.Remove(oldItem);
                    m_Triggers.Remove((ConsoleTrigger)oldItem.Tag);
                    lwTriggers.Items.Insert(items[i].Index, oldItem);
                    m_Triggers.Insert(items[i].Index, (ConsoleTrigger)oldItem.Tag);
                    items[i].Selected = true;
                    
                }
            }
        }

        private void bLoadExamples_Click(object sender, EventArgs e)
        {
            ConsoleTriggerTrayBalloon balloonExample = new ConsoleTriggerTrayBalloon();
            balloonExample.Name = "Show popup when a player joined a game";
            balloonExample.TriggerText = @"^\[GAME: (.*?)\] player \[(.*?)\] joined the game";
            balloonExample.BalloonText = "Player [$2] joined game [$1]";

            AddTrigger(balloonExample);
            m_Triggers.Add(balloonExample);

            balloonExample = new ConsoleTriggerTrayBalloon();
            balloonExample.Name = "Show popup when a player left a game";
            balloonExample.TriggerText = @"^\[GAME: (.*?)\] deleting player \[(.*?)\]: (.*)";
            balloonExample.BalloonText = "Player [$2] left game [$1]: $3";

            AddTrigger(balloonExample);
            m_Triggers.Add(balloonExample);

            balloonExample = new ConsoleTriggerTrayBalloon();
            balloonExample.Name = "Show popup when someone created a game";
            balloonExample.TriggerText = @"^\[LOCAL: (.*?)\] Creating (.*?) game \[(.*?)\] started by \[(.*?)\]\.";
            balloonExample.BalloonText = "Player [$4] hosted $2 game [$3]";

            AddTrigger(balloonExample);
            m_Triggers.Add(balloonExample);

            ConsoleTriggerFontChange textExample = new ConsoleTriggerFontChange(@"(?<=^\[)ERROR", Color.Red, true);
            textExample.Name = "Highlight errors";

            AddTrigger(textExample);
            m_Triggers.Add(textExample);

            ConsoleTriggerSendKeys gamemodeExample = new ConsoleTriggerSendKeys();
            gamemodeExample.TriggerText = @"^\[GAME: .*?DotA.*?(-\b\w+\b).*?\] finished loading";
            gamemodeExample.Message = "$1";
            gamemodeExample.Name = "Automatically type gamemode";

            AddTrigger(gamemodeExample);
            m_Triggers.Add(gamemodeExample);
            
            ConsoleTriggerSendKeys diExample = new ConsoleTriggerSendKeys();
            diExample.TriggerText = @"^\[GAME: .*?DotA.*?] finished loading";
            diExample.Message = "-di";
            diExample.Name = "Automatically type -di";

            AddTrigger(diExample);
            m_Triggers.Add(diExample);
        }
    }
}